<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class TechnologyApiController extends AbstractRestfulController
{
    public function getList()
    {			
		header('Access-Control-Allow-Origin: *');
		$technologyTable=$this->getServiceLocator()->get('Models\Model\TechnologyFactory');
		$technologyList = $technologyTable->getTechnologylist();
		if(($technologyList->count())!=0){
		$data = array();$i=0;
				foreach($technologyList as $questions){
					$tid=$questions->technology_id;
					$data[$i]['sno']=$i+1;
					$data[$i]['technology_name']= $questions->technology_name;
					$data[$i]['technology_image']= '<img src="../images/'.$questions->technology_image .'" size="100" width="50" >';
					$data[$i]['action']='<a href="javascript:void(0)" onclick = "edittechnologyFunction('.$tid.')">Edit</a> &nbsp;/&nbsp;<a href="javascript:void(0);" onClick="deleteTechnology('.$tid.')">Delete</a>';
					$i++;
				}
				$data['aaData'] = $data;
				echo json_encode($data['aaData']); exit;
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	 =>	1,
			));
		}	
    }
    public function get($course_id)
    {		
		header('Access-Control-Allow-Origin: *');
		$data=array();		
		$courseTable=$this->getServiceLocator()->get('Models\Model\TechnologyFactory');
		if($course_id==0){
			$editCourse= $courseTable->getTechnologylist()->toarray();
		}else{
			$editCourse= $courseTable->editCourse($course_id)->toarray();
		}
		if(count($editCourse)!=0){
			return new JsonModel(array(
				'editCourse'  => json_encode($editCourse),
				'value' 	  => 1,
			));
		}else{
			return new JsonModel(array(
				'output'  => 'no data',
				'value'   => 0,
			));
		}
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');	
		$technologyTable=$this->getServiceLocator()->get('Models\Model\TechnologyFactory');
		if($data['hidimage']!=""){
			$content=file_get_contents($data['hidimage']);
			$image=mt_rand();
			$fp = fopen("D:/wamp/www/pedia/pediahtmls/images/".$image.".jpg", "w");
			fwrite($fp, $content);
			fclose($fp);
			$images=$image.".jpg";
		}else{
			$images=$data['hid_dis_image'];
		}
		if($data['hid_tech_val']==0){
			$saveTechnology = $technologyTable->saveTechnology($data,$images);
			if($saveTechnology!=0){
				return new JsonModel(array(
					'output' 	=> 	'success',
					'courseId'	=>	$saveTechnology,
					'value' 	=> 	1,
				));
			}else{
				return new JsonModel(array(
					'output' => 'fail',
					'value' => 0,
				));
			}
		}else{
			$updateTechnology = $technologyTable->updateTechnology($data,$images);
			return new JsonModel(array(
				'output' 	=> 	'success',
				'value' 	=> 	1,
			));
		}
    }
    public function update($answer, $data)
    {
		header('Access-Control-Allow-Origin: *');	
		$resultsTable=$this->getServiceLocator()->get('Models\Model\ResultsFactory');
		$getQuestion=$resultsTable->getcheckData($data)->toArray();
		if(count($getQuestion)==0){
			$saveResults = $resultsTable->saveResults($data);
			if($saveResults!=0){
				return new JsonModel(array(
					'output'	 => 'success',
					'value'		 => 1,
				));
			}else{
				return new JsonModel(array(
					'output'	 => 'fail',
					'value'		 => 0,
				));
			}
		}else{
			$updateResults = $resultsTable->updateResults($data);
			return new JsonModel(array(
				'output'	 => 'fail',
				'value'		 => 0,
			));
		}
		
    }
    public function delete($tech_id)
    {
      $technologyTable=$this->getServiceLocator()->get('Models\Model\TechnologyFactory');
		$deleteTech = $technologyTable->deleteTech($tech_id);
			if($deleteTech!=0){
				return new JsonModel(array(
					'status'		=>	'success',
					'value'			=>	1,
				));	
			}else{
				return new JsonModel(array(
					'status'	=>	'fail',
					'value'		=>   0,
				));			
			}
    }
}